package com.healMyself.controller;

import com.healMyself.pojo.Record;
import com.healMyself.pojo.User;
import com.healMyself.service.RecordService;
import com.healMyself.service.impl.RecordServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@WebServlet("/record")
public class RecordServlet extends BaseServlet{
    private RecordService service = new RecordServiceImpl();

    public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        String time = req.getParameter("time");
        time=time.replace("T"," ");
        User user = (User)req.getSession().getAttribute("userObject");
        int userId = -1;
        if (user != null){
            userId = user.getId();
        }
        Timestamp timestamp = null;
        if (time!=null && !"".equals(time)){
            try {
                timestamp = new Timestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(time).getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Record record = new Record();
        try {
            BeanUtils.populate(record,map);
            record.setTheTime(timestamp);
            record.setUserId(userId);
            if (service.addRecord(record) > 0) {
                req.setAttribute("msg","操作成功！");
            }else {
                req.setAttribute("msg","操作失败！");
            }
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("userObject");
        int userId = -1;
        if (user != null)
            userId=user.getId();
        System.out.println(user);
        String inputMedicine = req.getParameter("medicine");
        String inputPlace = req.getParameter("place");
        String startTime = req.getParameter("startTime");
        startTime=timeFormatDeal(startTime);
        String deadline = req.getParameter("deadline");
        deadline=timeFormatDeal(deadline);
        Timestamp start = null;
        Timestamp end = null;
        try {
            start = startTime == null ? null :
        new Timestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(startTime).getTime());
            end = deadline == null ? null:
                    new Timestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(deadline).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Record> list = service.queryRecord(userId, inputMedicine, start, end, inputPlace);
        //把查到的records存入到域对象
        req.setAttribute("recordsList", list);
        //请求转发到查询页面上
        req.getRequestDispatcher("/record/query.jsp").forward(req,resp);
    }

    public String timeFormatDeal(String time){
        if (time == null || "".equals(time))
            return null;
        return time.replace("T"," ");
    }
}
